<?php

$sql = "SELECT id, name FROM navigation WHERE parent_id = 0 ORDER BY navigation_order";
$query_1 = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
$rows_1 = mysql_num_rows($query_1);

if($rows_1 > 0){

$category_list = '<ul id="category_list">';

	while($rs_1 = mysql_fetch_assoc($query_1)){

	$category_list.= '<li><a href="?id='.$rs_1['id'].'">'.$rs_1['name'].'</a>';
	
		// 2nd tier
		
		$sql = "SELECT id, name FROM navigation WHERE parent_id = '".$rs_1['id']."' ORDER BY navigation_order";
		$query_2 = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
		$rows_2 = mysql_num_rows($query_2);

		if($rows_2 > 0){
		
		$toggle_1 = 'expand';

			while($rs_2 = mysql_fetch_assoc($query_2)){
			
			$display_list_1 = ($_GET['id'] == $rs_2['id'] || $_GET['id'] == $rs_1['id']) ? ' style="display: block;"' : $display_list_1;
			$toggle_1 = ($_GET['id'] == $rs_2['id'] || $_GET['id'] == $rs_1['id']) ? 'collapse' : $toggle_1;
			$temp_category_list_1.= '<li><a href="?id='.$rs_2['id'].'">'.$rs_2['name'].'</a>';
			
				// 3rd tier
			
				$sql = "SELECT id, name FROM navigation WHERE parent_id = '".$rs_2['id']."' ORDER BY navigation_order";
				$query_3 = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
				$rows_3 = mysql_num_rows($query_3);
				
				if($rows_3 > 0){
				
				$toggle_2 = 'expand';
				
				while($rs_3 = mysql_fetch_assoc($query_3)){

				$third_tier = ($_GET['id'] == $rs_3['id']) ? true : $third_tier;
				$display_list_1 = ($_GET['id'] == $rs_3['id']) ? ' style="display: block;"' : $display_list_1;
				$display_list_2 = ($_GET['id'] == $rs_3['id'] || $_GET['id'] == $rs_2['id']) ? ' style="display: block;"' : $display_list_2;
				$toggle_1 = ($_GET['id'] == $rs_3['id']) ? 'collapse' : $toggle_1;
				$toggle_2 = ($_GET['id'] == $rs_3['id'] || $_GET['id'] == $rs_2['id']) ? 'collapse' : $toggle_2;
				$temp_category_list_2.= '<li><a href="?id='.$rs_3['id'].'">'.$rs_3['name'].'</a></li>';
				
				}				
				
				$temp_category_list_1.= ' <a title="'.ucfirst($toggle_2).'" class="toggle"><img src="images/'.$toggle_2.'.png" alt="'.ucfirst($toggle_2).'" /></a><ul'.$display_list_2.'>'.$temp_category_list_2.'</ul>';
				unset($display_list_2);
				unset($temp_category_list_2);
				unset($toggle_2);
				
				}
				
			$temp_category_list_1.='</li>';
				
			}

			$category_list.= ' <a title="'.ucfirst($toggle_1).'" class="toggle"><img src="images/'.$toggle_1.'.png" alt="'.ucfirst($toggle_1).'" /></a><ul'.$display_list_1.'>'.$temp_category_list_1.'</ul>';
			unset($display_list_1);
			unset($temp_category_list_1);
			unset($toggle_1);
		}
		
	$category_list.= '</li>';
	
	}

$category_list.= '</ul>';

}

echo $category_list;

?>